#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

/*
void test1(){
    double d = 12.34;
    int a = static_cast<int>(d);
    cout << a << endl;
    int* p = &a;
    //int address = static_cast<int>(p);
}

void test2(){
    int a = 10;
	int *p = reinterpret_cast<int*>(a);
    cout << p << endl;
}

typedef void(*FUNC)();
int DoSomething(int i){
    cout << "DoSomething: " << i << endl;
    return 0;
}

void test3(){
    FUNC f = reinterpret_cast<FUNC>(DoSomething);
    f();
}

void test4(){
    volatile const int a = 2;
    int* p = const_cast<int*>(&a);
    *p = 3;
    cout << a << endl;
    cout << *p << endl;
}

class A
{
public:
    virtual void f()
    {}
};

class B : public A
{};

void func(A* pa)
{
    B* pb1 = (B*)pa;               //不安全
    B* pb2 = dynamic_cast<B*>(pa); //安全

    cout << "pb1: " << pb1 << endl;
    cout << "pb2: " << pb2 << endl;
}

void test5(){
    A a;
    B b;
    func(&a);
    func(&b);
}

class AA
{
public:
    explicit AA(int a)
    {
        cout << "AA(int a)" << endl;
    }
    AA(const AA& a)
    {
        cout << "AA(const A& a)" << endl;
    }

private:
    int _a;
};

void test6(){
    AA a1(1);
    AA tmp(2);
    AA a2(tmp);
}

int main() {
    test6();
    return 0;
}
*/

//IO流

class Date{
    friend ostream& operator << (ostream& out, const Date& d);
    friend istream& operator >> (istream& in, Date& d);

public:
    Date(int year = 1, int month = 1, int day = 1)
        :_year(year)
        ,_month(month)
        ,_day(day){}

    operator bool() const{
        if (_year != 2024)
            return false;
        return true;
    }

private:
    int _year;
    int _month;
    int _day;
};

ostream& operator << (ostream& out, const Date& d){
    out << d._year << " " << d._month << " " << d._day;
    return out;
}

istream& operator >> (istream& in, Date& d){
    in >> d._year >> d._month >> d._day;
    return in;
}

void test1(){
    Date d(2024, 1, 16);
    cout << d << endl;
    while (d){
        cin >> d;
        cout << d << endl;
    }
}

void test2(){
    ofstream ofs("test.txt", ofstream::out | ofstream::app);
    ofs << "hello world" << endl;
    ofs << "hello world";
}

struct ServerInfo{
    string _add;
    int _port;
    Date _date;
};

struct ConfigManager{
public:
    ConfigManager(const char* fileName):_fileName(fileName){}

    void writeText(const ServerInfo& info){
        ofstream ofs(_fileName);
        ofs << info._add << endl;
        ofs << info._port << endl;
        ofs << info._date << endl;
    }

    void readText(ServerInfo& info){
        ifstream ifs(_fileName);
        ifs >> info._add;
        ifs >> info._port;
        ifs >> info._date;
    }

private:
    string _fileName;
};

void test3(){
    ServerInfo wInfo = {"192.0.0.1", 88, {2024, 1, 16}};
    string str;
    cin >> str;
    if (str == "write"){
        ConfigManager cm("test.txt");
        cm.writeText(wInfo);
    } else if (str == "read"){
        ServerInfo rInfo;
        ConfigManager cm("test.txt");
        cm.readText(rInfo);
        cout << rInfo._add << endl;
        cout << rInfo._port << endl;
        cout << rInfo._date << endl;
    }
}

void test4(){
    //ostringstream oss;
    ostringstream oss;
    oss << 100 << endl;
    oss << "hello";

    string str = oss.str();
    cout << str << endl;

    stringstream iss(str);
    int i;
    string s;
    iss >> i >> s;
    cout << i << endl;
    cout << s << endl;
}

struct ChatInfo{
    string _name;
    int _id;
    Date _date;
    string _msg;
};

void test5(){
    ChatInfo wInfo = {"Lu", 858585, {2024, 1, 16}, "study?"};
    stringstream oss;
    oss << wInfo._name << endl;
    oss << wInfo._id << endl;
    oss << wInfo._date << endl;
    oss << wInfo._msg << endl;
    string str = oss.str();
    cout << str;

    stringstream iss(str);
    ChatInfo rInfo;
    iss >> rInfo._name;
    iss >> rInfo._id;
    iss >> rInfo._date;
    iss >> rInfo._msg;
    cout << "----------------------------------------" << endl;
    cout << rInfo._name << "(" << rInfo._id << ")";
    cout << rInfo._date << endl;
    cout << rInfo._name << ":>> " << rInfo._msg << endl;
    cout << "----------------------------------------" << endl;
}

int main(){
    test5();
    return 0;
}